﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using MVC4App.Repositories.Core;
using MVC4App.Repositories.Core.Security;
using MVC4App.SecurityServiceReference;

namespace MVC4App.Repositories.Implementations.Security
{
    public class RoleRepository : RepositoryBase, IRoleRepository
    {
        public List<RoleDto> GetList(Criterion criterion = null)
        {
            var request = new RoleRequest().Prepare();
            request.LoadOptions = new[] { "Roles" };
            var response = SecurityServiceClient.GetRoles(request);
            Correlate(request, response);
            return response.Roles == null ? null : response.Roles.ToList();
        }

        public List<RoleDto> GetQuickSearchPaged(ref int totalPage, int currentPage = 1, string keyword = null)
        {
            throw new NotImplementedException();
        }

        public List<RoleDto> GetPaged(ref int totalPage, int currentPage = 1)
        {
            throw new NotImplementedException();
        }

        public int GetPageSize()
        {
            var request = new RoleRequest().Prepare();
            request.LoadOptions = new[] { "TotalPage" };

            var response = SecurityServiceClient.GetRoles(request);
            Correlate(request, response);
            return response.TotalPage;
        }

        public List<RoleDto> GetPaged(int currentPage)
        {
            var request = new RoleRequest().Prepare();
            request.LoadOptions = new[] { "Roles" };
            request.OptionTypes = OptionType.IsPaging;
            request.CurrentPage = currentPage - 1;

            var response = SecurityServiceClient.GetRoles(request);
            Correlate(request, response);
            return response.Roles == null ? null : response.Roles.ToList();
        }

        public List<RoleDto> GetByUnit(int unitId)
        {
            throw new NotImplementedException();
        }

        public RoleDto Get(int id)
        {
            var request = new RoleRequest().Prepare();
            request.LoadOptions = new[] { "Role" };
            request.Criteria = new RoleCriteria() { RoleId = id };

            var response = SecurityServiceClient.GetRoles(request);
            Correlate(request, response);
            return response.Role;
        }

        public int GetCount(Criterion criterion = null)
        {
            throw new NotImplementedException();
        }

        public string Insert(RoleDto role)
        {
            var request = new RoleRequest().Prepare();
            request.Action = "Create";
            request.Role = role;

            var response = SecurityServiceClient.SetRoles(request);
            Correlate(request, response);
            return response.Message;
        }

        public string Update(RoleDto role)
        {
            var request = new RoleRequest().Prepare();
            request.Action = "Update";
            request.Role = role;
            var response = SecurityServiceClient.SetRoles(request);
            Correlate(request, response);
            return response.Message;
        }

        public string Delete(int id)
        {
            var request = new RoleRequest().Prepare();
            request.Action = "Delete";
            request.Criteria = new RoleCriteria() { RoleId = id };
            var response = SecurityServiceClient.SetRoles(request);
            Correlate(request, response);
            return response.Message;
        }

        public string[] GetByUserId(int? id)
        {
            if (id == null) return null;
            var request = new RoleRequest().Prepare();
            request.LoadOptions = new[] { "RoleByUser" };
            request.Criteria = new RoleCriteria() { UserId = (int)id };
            var response = SecurityServiceClient.GetRoles(request);
            Correlate(request, response);
            return response.RolesByUsername;
        }

        public List<RoleInSiteDto> GetRoleInSite(int roleId)
        {
            var request = new RoleRequest().Prepare();
            request.LoadOptions = new[] { "RoleInSiteByRole" };
            request.Criteria = new RoleCriteria() { RoleId = roleId };
            var response = SecurityServiceClient.GetRoles(request);
            Correlate(request, response);
            return response.RoleInSite.ToList();
        }
        public List<RoleInSiteDto> GetRoleInSiteBySite(int siteId)
        {
            var request = new RoleRequest().Prepare();
            request.LoadOptions = new[] { "RoleInSiteBySite" };
            request.Criteria = new RoleCriteria() { SiteId = siteId };
            var response = SecurityServiceClient.GetRoles(request);
            Correlate(request, response);
            return response.RoleInSite.ToList();
        }
        public string GrantRolePermission(int roleId, List<RoleInSiteDto> objs)
        {
            var request = new RoleInSiteRequest().Prepare();
            request.Action = "Create";
            request.RoleInSites = objs.ToArray();
            request.Criteria = new RoleInSiteCriteria() { RoleId = roleId };
            var response = SecurityServiceClient.SetRoleInSites(request);
            Correlate(request, response);
            return response.Message;
        }
    }
}